import axios from "axios";
import { Message, MessageBox } from "element-ui";
import qs from "qs";

axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8";
// create an axios instance
const service = axios.create({
  baseURL: "/webapi", // api 的 base_url
  timeout: 30000, // request timeout
});

// request interceptor
service.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    // Do something with request error
    // console.log(error) // for debug
    Promise.reject(error);
  }
);

// response interceptor
service.interceptors.response.use(
  (response) => {
    const res = response.data;
    if (res.code === 501) {
      MessageBox.alert("系统未登录，请重新登录", "错误", {
        confirmButtonText: "确定",
        type: "error",
      }).then(() => {
        //退出登录
        // store.dispatch('FedLogOut').then(() => {
        //   const url = delParam('token') || window.location.href
        //   window.location.replace()// 否则全部重定向到登录页
        // })
      });
      return Promise.reject("error");
    } else if (res.code === 502) {
      MessageBox.alert("系统内部错误，请联系管理员维护", "错误", {
        confirmButtonText: "确定",
        type: "error",
      });
      return Promise.reject("error");
    } else if (res.code === 503) {
      MessageBox.alert("请求业务目前未支持", "警告", {
        confirmButtonText: "确定",
        type: "error",
      });
      return Promise.reject("error");
    } else if (res.code === 504) {
      MessageBox.alert("更新数据已经失效，请刷新页面重新操作", "警告", {
        confirmButtonText: "确定",
        type: "error",
      });
      return Promise.reject("error");
    } else if (res.code === 505) {
      MessageBox.alert("更新失败，请再尝试一次", "警告", {
        confirmButtonText: "确定",
        type: "error",
      });
      return Promise.reject("error");
    } else if (res.code === 506) {
      MessageBox.alert("没有操作权限，请联系管理员授权", "错误", {
        confirmButtonText: "确定",
        type: "error",
      });
      return Promise.reject("error");
    } else if (response.code == 401) {
      MessageBox.alert(res.message || "系统未登录，请重新登录", "错误", {
        confirmButtonText: "确定",
        type: "error",
      }).then(() => {
        // store.dispatch('FedLogOut').then(() => {
        //   const url = delParam('token') || window.location.href
        //   window.location.replace()// 否则全部重定向到登录页
        // })
      });
      return Promise.reject("error");
    } else if (
      res.code &&
      res.code !== 200 &&
      res.code != "000000" &&
      res.code != 0
    ) {
      /* Message.error(res.errmsg || '出错了')*/
      // 非5xx的错误属于业务错误，留给具体页面处理
      return Promise.reject(response.data);
    } else if (!res.code) {
      return response.data;
    } else {
      return response.data;
    }
  },
  (error) => {
    if (error.response && error.response.status == "401") {
      MessageBox.alert(
        error.response.data.message || "系统未登录，请重新登录",
        "错误",
        {
          confirmButtonText: "确定",
          type: "error",
        }
      ).then(() => {
        //退出
        // store.dispatch('FedLogOut').then(() => {
        //   const url = delParam('token') || window.location.href
        //   window.location.replace()// 否则全部重定向到登录页
        //})
      });
      return Promise.reject(error);
    }
    // console.log('err' + error)// for debug
    Message({
      message: "连接超时（后台不能连接，请联系系统管理员）",
      type: "error",
      duration: 5 * 1000,
    });
    return Promise.reject(error);
  }
);

export default service;
